package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

/**
 * Created by ThinkPad on 2017/9/7.
 */
public class GetPrePaymentsTransfer_old extends AbstractTransfer {

    public GetPrePaymentsTransfer_old(String method, Client client, String messageMethod)  {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String idNo = (String) params.get("idNo");
        Document document = getTplDoc("GetPrePayments");
        Element dataRow = document.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        dataRow.addAttribute("ID_NO", idNo).addAttribute("PATIENT_ID", "").addAttribute("RCPT_NO", "");
        return document.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        List<Map<String, Object>> payRecordMaps = new ArrayList<>();
        if (executeStatus.equals("0")) {
            List<Element> payDataRows = document.getRootElement().element("ReturnData").element("Data").elements();
            List<Map<String, Object>> dataList = new ArrayList<>();
            if (payDataRows != null && payDataRows.size() > 0) {
                for (Element dataRow : payDataRows) {
                    PayRecordsInnerMessage payRecordsInnerMessage = new PayRecordsInnerMessage(dataRow);
                    Map<String, Object> dataMap = new HashMap<>();
                    dataMap.put("patientId", payRecordsInnerMessage.getPatientId());
                    dataMap.put("visitId", payRecordsInnerMessage.getVisitId());
                    dataMap.put("idNo", payRecordsInnerMessage.getIdNo());
                    dataMap.put("rcptNo", payRecordsInnerMessage.getRcptNo());
                    dataMap.put("amount", payRecordsInnerMessage.getAmount());
                    dataMap.put("payWay", payRecordsInnerMessage.getPayWay());
                    dataMap.put("transactType", payRecordsInnerMessage.getTransactType());
                    dataMap.put("winNo", payRecordsInnerMessage.getWinNo());
                    dataMap.put("payDateTime", payRecordsInnerMessage.getPayDateTime());
                    dataMap.put("cardNo", payRecordsInnerMessage.getCardNo());
                    dataMap.put("cardType", payRecordsInnerMessage.getCardType());
                    dataMap.put("tranNo", payRecordsInnerMessage.getTranNo());
                    dataMap.put("streamNo", payRecordsInnerMessage.getStreamNo());
                    dataMap.put("reference", payRecordsInnerMessage.getReference());
                    dataMap.put("bankCode", payRecordsInnerMessage.getBankCode());
                    dataMap.put("terminalNumber", payRecordsInnerMessage.getTerminalNumber());
                    dataMap.put("prepaymentBalance", payRecordsInnerMessage.getPrepaymentBalance());
                    dataList.add(dataMap);
                }
            }
            resMap.put("list",dataList);
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
      } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        }
        return resMap;
    }

    private class PayRecordsInnerMessage implements InnerMessage {

        private String patientId;
        private String visitId;
        private String idNo;
        private String rcptNo;
        private String amount;
        private String payWay;
        private String transactType;
        private String winNo;
        private String payDateTime;
        private String cardNo;
        private String cardType;
        private String tranNo;
        private String streamNo;
        private String reference;
        private String bankCode;
        private String terminalNumber;
        private String prepaymentBalance;



        public PayRecordsInnerMessage(Element element) {

            this.patientId = element.attributeValue("PATIENT_ID");
            this.visitId = element.attributeValue("VISIT_ID");
            this.idNo = element.attributeValue("ID_NO");
            this.rcptNo = element.attributeValue("RCPT_NO");
            this.amount = element.attributeValue("AMOUNT");
            this.payWay = element.attributeValue("PAY_WAY");
            this.transactType = element.attributeValue("TRANSACT_TYPE");
            this.winNo = element.attributeValue("WIN_NO");
            this.payDateTime = element.attributeValue("PAY_DATE_TIME");
            this.cardNo = element.attributeValue("CARD_NO");
            this.cardType = element.attributeValue("CARD_TYPE");
            this.tranNo = element.attributeValue("TRAN_NO");
            this.streamNo = element.attributeValue("STREAM_NO");
            this.reference = element.attributeValue("REFERENCE");
            this.bankCode = element.attributeValue("BANK_CODE");
            this.terminalNumber = element.attributeValue("TERMINAL_NUMBER");
            this.prepaymentBalance = element.attributeValue("PREPAYMENT_BALANCE");
        }

        public String getPatientId() {
            return patientId;
        }

        public String getVisitId() {
            return visitId;
        }

        public String getIdNo() {
            return idNo;
        }

        public String getRcptNo() {
            return rcptNo;
        }

        public String getAmount() {
            return amount;
        }

        public String getPayWay() {
            return payWay;
        }

        public String getTransactType() {
            return transactType;
        }

        public String getWinNo() {
            return winNo;
        }

        public String getPayDateTime() {
            return payDateTime;
        }

        public String getCardNo() {
            return cardNo;
        }

        public String getCardType() {
            return cardType;
        }

        public String getTranNo() {
            return tranNo;
        }

        public String getStreamNo() {
            return streamNo;
        }

        public String getReference() {
            return reference;
        }

        public String getBankCode() {
            return bankCode;
        }

        public String getTerminalNumber() {
            return terminalNumber;
        }

        public String getPrepaymentBalance() {
            return prepaymentBalance;
        }
    }


}
